package com.gzbugu.polytechsasia.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gzbugu.polytechsasia.dao.DatabaseLinkDAO;
import com.gzbugu.polytechsasia.dao.IArticleDAO;
import com.gzbugu.polytechsasia.dao.SQLExecuter;
import com.gzbugu.polytechsasia.vo.Article;

/**
 * Article dao
 * 
 * @author gauze
 */
public class ArticleDAO implements IArticleDAO {

	private Logger log = LoggerFactory.getLogger(this.getClass());

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.gzbugu.polytechsasia.dao.impl.IArticleDAO#save(com.gzbugu.polytechsasia
	 * .vo.Article)
	 */
	public void save(Article article) throws Exception {
		try {
			log.debug("save article " + article);
			String prepareSql = "INSERT INTO Article (content,title,type) VALUES(?,?,?)";
			List<Object> params = new ArrayList<Object>();
			params.add(article.getContent());
			params.add(article.getTitle());
			params.add(article.getType());
			SQLExecuter.update(prepareSql, params);
			log.debug("save successful");
		} catch (Exception e) {
			log.error("save failed", e);
			throw e;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.gzbugu.polytechsasia.dao.impl.IArticleDAO#delete(com.gzbugu.polytechsasia
	 * . vo.Article)
	 */
	public void delete(Article article) throws Exception {
		try {
			log.debug("delete article " + article);
			String prepareSql = "DELETE FROM Article WHERE id=?";
			List<Object> params = new ArrayList<Object>();
			params.add(article.getId());
			SQLExecuter.update(prepareSql, params);
			log.debug("delete successful");
		} catch (Exception e) {
			log.error("delete failed", e);
			throw e;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.gzbugu.polytechsasia.dao.impl.IArticleDAO#findById(int)
	 */
	public Article findById(int id) throws SQLException {
		log.debug("find article by id " + id);
		String prepareSql = "SELECT * FROM Article WHERE id = ?";
		Connection conn = DatabaseLinkDAO.getConnection();
		try {
			PreparedStatement statement = conn.prepareStatement(prepareSql);
			statement.setInt(1, id);
			ResultSet resultSet = statement.executeQuery();
			Article article = new Article();
			if (resultSet.next()) {
				article.setId(resultSet.getInt(1));
				article.setContent(resultSet.getString(2));
				article.setTitle(resultSet.getString(3));
				article.setType(resultSet.getInt(4));
				log.debug("find article successful");
				DatabaseLinkDAO.free(resultSet, statement, conn);
				return article;
			} else {
				log.debug("find article failed and return null : not exist");
				DatabaseLinkDAO.free(resultSet, statement, conn);
				return null;
			}
		} catch (SQLException e) {
			log.error("find article failed", e);
			DatabaseLinkDAO.free(null, null, conn);
			throw e;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.gzbugu.polytechsasia.dao.IArticleDAO#saveOrUpdate(com.gzbugu.
	 * polytechsasia.vo.Article)
	 */
	public void saveOrUpdate(Article article) throws Exception {
		log.debug("save or update article " + article);
		try {
			if (findById(article.getId()) == null)
				save(article);// 不存在，保存
			else {
				update(article);// 存在，更新
			}
		} catch (SQLException e) {
			log.error("save or update article failed", e);
			throw e;
		} catch (Exception e) {
			log.error("save or update article failed", e);
			throw e;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.gzbugu.polytechsasia.dao.IArticleDAO#update(com.gzbugu.polytechsasia
	 * .vo.Article)
	 */
	public void update(Article article) throws Exception {
		log.debug("update article " + article);
		String prepareSql = "UPDATE Article SET content = ?,title = ?, type = ? WHERE id = ?";
		List<Object> params = new ArrayList<Object>();
		try {
			if (article != null) {
				params.add(article.getContent());
				params.add(article.getTitle());
				params.add(new Integer(article.getType()));
				params.add(new Integer(article.getId()));
				SQLExecuter.update(prepareSql, params);
				log.debug("update article successful");
			}
		} catch (Exception e) {
			log.error("update article failed", e);
			throw e;
		}
	}
}
